﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using TigerAndGoats.Control;
using TigerAndGoats.Framework;
using TigerAndGoats.Resources;

namespace TigerAndGoats.Sprites
{
    public class Goat: Piece
    {
        public Goat()
        {
            this._image = new System.Drawing.Bitmap(@"Resources\goat.png");
            this._image_Gray = new System.Drawing.Bitmap(@"Resources\goat_Gray.png");
        }


        public override void Draw(Graphics graphics)
        {
            if (_location != null)
            {
                if (GameControl.currentRound == GameControl.Round.Goats)
                {
                    DrawMyImage(graphics, _image);
                }
                else
                {
                    DrawMyImage(graphics, _image_Gray);
                }
            }
        }

        private void DrawMyImage(Graphics graphics, Bitmap myImage)
        {
            graphics.DrawImage(myImage, new Rectangle(
                                            GetPieceLocation(GetPointByOrdinate(_location.GetOrdinate())
                                            , GlobalSettings.PieceSizeGoat(GetPutupRatio()))
                                            , GlobalSettings.PieceSizeGoat(GetPutupRatio())
                                            )
                                        );
        }

        public override void PutUp(Action action = null)
        {
            if (Piece.currentPutUp == Piece.Empty
                && GameControl.CurrentGameState == GameControl.GameState.InProcess
                && GameControl.currentRound == GameControl.Round.Goats)
            {
                if (GameControl.GoatsOnHand > 0)
                {
                    GameControl.SetGeneralTip(UITipText.GOAT_CANNOT_MOVE_BEFORE_GROUP_STILL_EXIST);
                    return;
                }

                List<Tuple<GraphNode, DestinationTip>> actualReachablePaths = new List<Tuple<GraphNode, DestinationTip>>();

                var allReachableNodes = this._location.GetAllReachableNodes();
                // Show next step tip
                foreach (var reachableNode in allReachableNodes)
                {
                    if (!(reachableNode.GetPiece() is Goat) && !(reachableNode.GetPiece() is Tiger))
                    {
                        var reachableDestination = new DestinationTip() { FromPiece = this };
                        _reachableDestinations.Add(reachableDestination);
                        actualReachablePaths.Add(new Tuple<GraphNode, DestinationTip>(reachableNode, reachableDestination));
                    }
                }

                if (actualReachablePaths.Count > 0)
                {
                    base.PutUp(new Action(() =>
                    {
                        foreach (var pathPairTuple in actualReachablePaths)
                        {
                            pathPairTuple.Item1.SetPiece(pathPairTuple.Item2);
                        }
                    }));
                }
            }
        }
    }
}
